package com.bw.controller;

import com.bw.entity.Dict;
import com.bw.entity.HouseAddParam;
import com.bw.entity.HouseInfo;
import com.bw.entity.HouseQuery;
import com.bw.mapper.HouseMapper;
import com.bw.service.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.interfaces.PBEKey;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class HouseController {

    @Autowired
    HouseMapper houseMapper;
    @Autowired
    HouseService houseService;


    @RequestMapping("/list.do")
    public String list(HouseQuery query, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        //1. 下拉框
        // 查询所有的省份 (不需要查城市，因为城市的数据 是二级联动出来的)
        List<Dict> provinceList = houseMapper.listProvince();
        model.addAttribute("provinceList", provinceList);

        //2. 分页查询
        PageHelper.startPage(pageNum, 5);
        List<HouseInfo> list = houseMapper.selectPage(query);
        PageInfo<HouseInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        //3. 条件回显
        model.addAttribute("query", query);
        // 如果搜索条件中有 城市 才回显城市
        if (query.getCityId() != null) {
            // 根据搜索条件里的省份 查询所有的城市
            List<Dict> cityList = houseMapper.getCityByProvinceId(query.getProvinceId());
            model.addAttribute("cityList", cityList);
        }
        return "list";
    }


    @RequestMapping("/getCity.do")
    @ResponseBody
    public List<Dict> getCity(Integer provinceId) {

        return houseMapper.getCityByProvinceId(provinceId);
    }

    @RequestMapping("/getQu.do")
    @ResponseBody
    public List<Dict> getQu(Integer cityId) {

        return houseMapper.getQuByCityId(cityId);
    }

    @RequestMapping("/addPage.do")
    public String addPage(Model model) {
        List<Dict> provinceList = houseMapper.listProvince();
        model.addAttribute("provinceList", provinceList);

        return "add";
    }

    @RequestMapping("/add.do")
    public String add(@Valid HouseAddParam addParam, BindingResult result, Model model) throws IOException {
        if (result.hasErrors()) {
            model.addAttribute("error", result.getAllErrors().get(0).getDefaultMessage());

            List<Dict> provinceList = houseMapper.listProvince();
            model.addAttribute("provinceList", provinceList);
            return "add";
        }

        String name = addParam.getName();
        if (name.length() > 10) {
            model.addAttribute("error", "名称长度大于10位数");
            List<Dict> provinceList = houseMapper.listProvince();
            model.addAttribute("provinceList", provinceList);
            return "add";

        }
        // 处理图片
        MultipartFile picFile = addParam.getPicFile();// 那张图片
        String filename = picFile.getOriginalFilename(); //获取图片的名字
        // 创建一个空文件  构造器的入参是： 图片在磁盘里的绝对路径
        File file = new File("/Users/hucongcong/Documents/bawei/season2/2111A/month33-tu/web/src/main/webapp/static/img/" + filename);

        picFile.transferTo(file);// 把图片 传输到一个空文件里；

        String url = "http://localhost:8080/static/img/" + filename; // 图片的访问地址
        addParam.setPicUrl(url); // 往MySQL里存储图片的访问路径

        houseService.add(addParam);
        //发送短信给管理员用户(在什么时间添加了楼盘信息)
        Date date = new Date();
        ZzyUtils.sendSms("1212132321", date + "添加了楼盘", 5);
        return "redirect:list.do";
    }

}
